package com.gzs.learn.admin.web.controller.api;

import com.gzs.learn.common.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "webapi接口")
@RequestMapping("/web")
public class WebApiController {

    @GetMapping("test")
    public AjaxResult testWeb() {
        // 模拟接口耗时
        int random = java.util.concurrent.ThreadLocalRandom.current().nextInt(10, 20);
        try {
            TimeUnit.MILLISECONDS.sleep(random);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (random % 19 == 0) {
            return AjaxResult.error(1, "biz error");
        }
        if (random % 17 == 0) {
            // 系统异常模拟
            throw new RuntimeException("just throw runtime exp");
        }
        return AjaxResult.success("web-api");
    }
}
